package com.sl.au.service.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.StorageProperties;
import com.sl.au.repository.BusinesssellerBaseInfoRepository;
import com.sl.au.service.BussessllerbaseinfoExcelloadService;
import com.sl.au.service.ExcelService;
import com.sl.au.service.StorageException;

@Service("BussessllerbaseinfoExcelloadService")
public class BussessllerbaseinfoExcelloadServiceImpl extends BaseServiceImpl implements BussessllerbaseinfoExcelloadService{
	
	//private static Logger logger = LoggerFactory.getLogger(AttachmentServiceImpl.class);
	private final Path rootLocation;
	private final Long K = 1024l, M = K * 1024l, G = M * 1024l;
	
	@Autowired
	ExcelService excelService;
	
	@Autowired
	BusinesssellerBaseInfoRepository businesssellerBaseInfo;
	
	@Autowired
	public BussessllerbaseinfoExcelloadServiceImpl(StorageProperties properties) {
		this.rootLocation = Paths.get(properties.getLocation());
	}
	@SuppressWarnings("finally")
	@Override
	public String upload(MultipartFile file) {
		// TODO Auto-generated method stub
		if (null == file)
			return null;
		String savedFileName = null;
		String fileName = file.getOriginalFilename();
		try {
			if (file.isEmpty()) {
				throw new StorageException("无法存储空文件" + fileName);
			}
			savedFileName = java.util.UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
			Path path = this.rootLocation.resolve(savedFileName);
			Files.copy(file.getInputStream(), path);

		} catch (IOException e) {
			throw new StorageException("存储附件失败" + file.getOriginalFilename(), e);
		} finally {
			return savedFileName;
		}
	}

	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public long importExcel(String fileName) {
		// TODO Auto-generated method stub
		long count = 0;
		if (!StringUtils.hasText(fileName))
			return count;
		Map<String, Object> collections = new HashMap<>();
		String entityName = "businesssellerBaseInfo";// 导入时用businesssellerBaseInfo.xml模版

		excelService.importExcel(entityName, collections, fileName);
		if (collections.size() < 1 || collections.get("items") == null)
			return count;
		
		List<BusinesssellerBaseInfo> rows = (List<BusinesssellerBaseInfo>) collections.get("items");
//		
				this.businesssellerBaseInfo.save(rows);
		return count;
	}

}
